package com.chunxuan.langquan.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.chunxuan.langquan.R;
import com.chunxuan.langquan.dao.bean.CourseDetail;
import com.chunxuan.langquan.support.util.GlideUtil;
import com.chunxuan.langquan.ui.activity.CourseDetailActivity;

import java.util.ArrayList;
import java.util.List;

public class CourseRecordAdapter extends RecyclerView.Adapter {
    private List<CourseDetail.CourseInfo> courseInfoList=new ArrayList<>();
    private Context mContext;
    public CourseRecordAdapter(Context context, List<CourseDetail.CourseInfo> courseInfoList) {
        this.mContext = context;
        this.courseInfoList = courseInfoList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.item_course_record, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(v);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        MyViewHolder myViewHolder= (MyViewHolder) holder;
        if(null!=courseInfoList&&courseInfoList.size()>0){
            CourseDetail.CourseInfo courseInfo=courseInfoList.get(position);
            myViewHolder.tvCourseTitle.setText(courseInfo.getTitle());
            GlideUtil.loadImg(courseInfo.getKecheng_image(), myViewHolder.ivCourseImage);
            myViewHolder.myItemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, CourseDetailActivity.class);
                    intent.putExtra("grade_category_id", courseInfo.getGrade_category_id() + "");
                    intent.putExtra("subject_category_id", courseInfo.getSubject_category_id() + "");
                    intent.putExtra("class_category_id", courseInfo.getClass_category_id() + "");
                    intent.putExtra("course_id", courseInfo.getId() + "");
                    mContext.startActivity(intent);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return courseInfoList==null?0:courseInfoList.size();
    }
    public static class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView ivCourseImage;
        private TextView tvCourseTitle;
        private  View myItemView;;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            ivCourseImage=itemView.findViewById(R.id.iv_course_image);
            tvCourseTitle=itemView.findViewById(R.id.tv_course_title);
            myItemView=itemView;
        }
    }
}
